package com.zoo.boot.api;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *
 *  layui 的标准
 * @Author: WangB
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> implements Serializable {
    private boolean  success;
    private long  code;
    private String msg;
    private T data;

    //总记录数
    private long count;


    public CommonResult(long code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public CommonResult(long code,String msg,boolean success,T data){
        this.code = code;
        this.msg = msg;
        this.success = success;
        this.data = data;
    }

    /**
     * 设置 记录数
     * @param count
     * @return
     */

    public CommonResult setCount(long count){
        this.count = count;
        return  this;
    }



    /**
     * 返回操作成功信息
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> success(){
        return  success(null);
    }


    /**
     * 返回操作成功信息<包含数据>
     * @param data
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> success(T data){
        return  new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),true,data);
    }


    /**
     * 返回操作成功信息<包含数据和自定义提示信息>
     * @param data
     * @param message
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T data, String message){
        return   new CommonResult<>(ResultCode.SUCCESS.getCode(),message,true,data);
    }


    /**
     * 返回表格数据的状态码和信息
     * @param data
     * @param <T>
     * @return
     */

    public static <T> CommonResult<T> tableSuccess(T data){
        return   new CommonResult<>(ResultCode.TABLE_SUCCESS.getCode(), ResultCode.TABLE_SUCCESS.getMessage(),true,data);
    }


    /**
     * 返回操作失败信息
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> fail(){
        return  fail(null,ResultCode.FAIL.getMessage());
    }

    /**
     * 返回操作失败信息<包含数据>
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> fail(T data){
        return  new CommonResult<>(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMessage(),data);
    }

    /**
     * 返回操作失败信息<包含数据和自定义提示信息>
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> fail(T data, String message){
        return  new CommonResult<>(ResultCode.FAIL.getCode(),message,data);
    }


    /**
     *  公共返回操作失败信息 方法
     * @param iErrorCode
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> fail(IErrorCode iErrorCode){
        return  new CommonResult<>(iErrorCode.getCode(),iErrorCode.getMessage(),null);
    }


    /**
     * 验证失败
     * @param <T>
     * @return
     */
    public  static <T> CommonResult<T> validateFail(){
        return  fail(ResultCode.VALIDATE_FAILED);
    }

}
